package com.heima.wemedia.scan.job;

import com.heima.model.wemedia.entity.WmNews;
import com.heima.wemedia.scan.dao.WmNewsDao;
import com.heima.wemedia.scan.service.WmNewsAutoScanService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class XxlArticleJob {

    @Resource
    private WmNewsDao wmNewsDao;

    @Resource
    private WmNewsAutoScanService wmNewsAutoScanService;

    /**
     * 扫描审核通过待发布 , 且到了发布时间的文章数据
     */
    @XxlJob("newsAutoScanJob")
    public void scanWmNews() {
        log.info("开始扫描数据库需要发布的文章数据");
        //查询需要发布的文章列表
        List<WmNews> wmNewsList = wmNewsDao.queryReleaseNews();
        //发布文章
        wmNewsList.forEach(wmNews -> wmNewsAutoScanService.saveArticle(wmNews));

        log.info("文章发布完成");
    }
}
